from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from django.contrib.auth.models import Group
from meiduo_admin.utils.pagination import MeiduoPagination
from meiduo_admin.serializers.groups import GroupSerializer, GroupSimpleSerializer


class GroupView(ModelViewSet):
    queryset = Group.objects.all()

    # serializer_class = GroupSerializer
    def get_serializer_class(self):
        if self.action == 'list':
            serializer_class = GroupSerializer
        elif self.action == 'simple':
            serializer_class = GroupSimpleSerializer
        return serializer_class

    pagination_class = MeiduoPagination

    @action(methods=['get'], detail=False)
    def simple(self, reauest, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
